package com.itzb.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itzb.api.constant.ApiMessageConstant;
import com.itzb.asone.application.BaseController;
import com.itzb.asone.constant.SystemParamConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysApi;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysApiService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysParamService;
import com.itzb.blog.inter.BlogClassInterface;
import com.itzb.blog.inter.BlogInterface;
import com.itzb.blog.inter.BlogMessageInterface;
import com.itzb.blog.inter.WebInterface;
import com.itzb.blog.model.ApiResult;
import com.itzb.blog.model.BlogClassResponseModel;
import com.itzb.blog.model.BlogMessageRequestModel;
import com.itzb.blog.model.BlogMessageResponseModel;
import com.itzb.blog.model.BlogRequestModel;
import com.itzb.blog.model.BlogResponseModel;
import com.itzb.blog.model.WebInterResponseModel;

@Controller
@RequestMapping(value = "api")
public class ApiController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysApiService sysApiService;
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private BlogInterface blogInterface;
	@Autowired
	private BlogClassInterface blogClassInterface;
	@Autowired
	private WebInterface webInterface;
	@Autowired
	private ISysParamService sysParamService;
	@Autowired
	private BlogMessageInterface blogMessageInterface;

	
	@RequestMapping(value = "")
	public String api(HttpServletRequest request,HttpServletResponse response) {
		Criteria<SysApi> param = new Criteria<SysApi>();
		List<SysApi> list = sysApiService.queryPage(param);
		String domain = sysParamService.getParamValue(SystemParamConstant.domain);
		for (int i = 0; i < list.size(); i++) {
			String apiUrl = domain + list.get(i).getApiUrl();
			list.get(i).setApiUrl(apiUrl);
		}
		return "api/apilist";
	}
	
	@ResponseBody
	@RequestMapping(value = "apilist")
	public Object apiList(HttpServletRequest request,HttpServletResponse response) {
		Criteria<SysApi> param = new Criteria<SysApi>();
		List<SysApi> list = sysApiService.queryPage(param);
		String domain = sysParamService.getParamValue(SystemParamConstant.domain);
		for (int i = 0; i < list.size(); i++) {
			String apiUrl = domain + list.get(i).getApiUrl();
			list.get(i).setApiUrl(apiUrl);
		}
		return list;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "apiurl")
	public String apiUrl(HttpServletRequest request,HttpServletResponse response) {
		String apiCode = request.getParameter("apiCode");
		String url = sysApiService.getUrlByCode(apiCode);
		String domain = sysParamService.getParamValue(SystemParamConstant.domain);
		return domain + url;
	}
	
	
	/**
	 * 获取博客设置
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getblogseting")
	public Object getBlogString(HttpServletRequest request,HttpServletResponse response) throws Exception {
		WebInterResponseModel webInterResponseModel = webInterface.getHeadContent();
		return webInterResponseModel;
	}
	
	/**
	 * 获取博客分类
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getblogclass")
	public Object getBlogClass(HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<BlogClassResponseModel> blogClassList = blogClassInterface.getBlogClass();
		return blogClassList;
	}
	
	
	/**
	 * 获取博客文章列表
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getbloglist")
	public Object getBlogList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		BlogRequestModel blogRequestModel = new BlogRequestModel();
		if(request.getParameter("page") != null && request.getParameter("pageSize") != null){
			blogRequestModel.setPage(Integer.parseInt(request.getParameter("page")));
			blogRequestModel.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		}
		if(request.getParameter("classId") != null){
			blogRequestModel.setClassId(request.getParameter("classId"));
		}
		if(request.getParameter("orderBy") != null){
			blogRequestModel.setOrderBy(request.getParameter("orderBy"));
		}
		List<BlogResponseModel> blogList = blogInterface.getBlogList(blogRequestModel);
		return blogList;
	}
	
	/**
	 * 获取推荐博客文章列表
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getblogtjlist")
	public Object getBlogTjList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		BlogRequestModel blogRequestModel = new BlogRequestModel();
		if(request.getParameter("page") != null && request.getParameter("pageSize") != null){
			blogRequestModel.setPage(Integer.parseInt(request.getParameter("page")));
			blogRequestModel.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		}
		List<BlogResponseModel> blogList = blogInterface.getBlogTjList(blogRequestModel);
		return blogList;
	}
	
	
	
	/**
	 * 获取博客文章
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getblog")
	public Object getBlog(HttpServletRequest request,HttpServletResponse response) throws Exception {
		BlogRequestModel blogRequestModel = new BlogRequestModel();
		if(request.getParameter("id") != null){
			blogRequestModel.setId(request.getParameter("id"));
		}
		BlogResponseModel blog = blogInterface.getBlog(blogRequestModel);
		return blog;
	}
	
	
	/**
	 * 发布或修改博客文章
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyblog", method = { RequestMethod.POST } )
	public Object modifyBlog(HttpServletRequest request,HttpServletResponse response) throws Exception {
		BlogRequestModel blogRequestModel = new BlogRequestModel();
		ApiResult apiResult = ApiResult.newInstance();
		if(request.getParameter("id") != null){
			blogRequestModel.setId(request.getParameter("id"));
		}
		if(request.getParameter("author") != null){
			logger.error(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_01));
			return apiResult.declareFailure(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_01));
		}
		if(request.getParameter("classId") != null){
			logger.error(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_02));
			return apiResult.declareFailure(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_02));
		}
		if(request.getParameter("title") != null){
			logger.error(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_03));
			return apiResult.declareFailure(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_03));
		}
		if(request.getParameter("state") != null){
			logger.error(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_04));
			return apiResult.declareFailure(messageService.message(ApiMessageConstant.API_BLOG_MODIFY_04));
		}
		
		blogRequestModel.setAuthor(request.getParameter("author"));
		blogRequestModel.setClassId(blogRequestModel.getClassId());
		blogRequestModel.setContent(blogRequestModel.getContent());
		blogRequestModel.setHighlightStyle(blogRequestModel.getHighlightStyle());
		blogRequestModel.setRecommendOrder(request.getParameter("recommendOrder"));
		blogRequestModel.setTj(blogRequestModel.getTj());
		blogRequestModel.setTag(request.getParameter("tag"));
		blogRequestModel.setTitle(request.getParameter("title"));
		blogRequestModel.setState(request.getParameter("state"));
		apiResult = blogInterface.modifyBlog(blogRequestModel);
		return apiResult.declareSuccess();
	}
	
	
	/**
	 * 获取碎言碎语列表
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/getblogmessagelist")
	public Object getBlogMessageList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		BlogMessageRequestModel blogMessageRequestModel = new BlogMessageRequestModel();
		if(request.getParameter("page") != null && request.getParameter("pageSize") != null){
			blogMessageRequestModel.setPage(Integer.parseInt(request.getParameter("page")));
			blogMessageRequestModel.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		}
		List<BlogMessageResponseModel> list = blogMessageInterface.getBlogMessageList(blogMessageRequestModel);
		return list;
	}
}
